﻿using ChoMy.Helpers;
using ChoMy.Models;
using ChoMy.Models.Data;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ChoMy.Controllers
{
    public class HomeController : Controller
    {
        private ChoMyContext db = new ChoMyContext();
        private StringHelper strHp = new StringHelper();

        public ActionResult Index()
        {
            ApplicationUser user = new ApplicationDbContext().Users.FirstOrDefault();

            Category category = db.Categories
                .FirstOrDefault(x => x.ParentId == null
                    && !x.IsDelete);

            if (category == null)
            {
                return RedirectToAction("Maintenance", "Error");
            }

            return RedirectToAction("Index", "Event", new { categoryNameId = "nu" });
        }

        public string PartialViewToString(string viewName, object model)
        {
            ViewData.Model = model;
            using (var sw = new StringWriter())
            {
                var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
                var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
                viewResult.View.Render(viewContext, sw);
                viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
                return sw.GetStringBuilder().ToString();
            }
        }
    }
}  